* This file creates descriptives for audits and audit outcomes
* VV/IV is needed (i) to calculate audit probabilities and (ii) to calculate lags between filing and audit start


* Merge CQI and VV/IV
*********************

use "${CQIoutdata}\CQI_enquiry_incometax.dta", clear

* Merge in VV/IV
sort utr_no tax_year
isid utr_no tax_year
capture drop _merge
merge 1:1 utr_no tax_year using "${VVoutdata}\ValidView_1997_2012.dta"


* Number of self-assessment taxpayers
*************************************

preserve

keep if inlist(_merge, 2, 3)
collapse (count) utr_no, by(tax_year) fast

restore


* A bit more processing
***********************

* Keep relevant years
keep if inrange(tax_year, 1999, 2009)

* Drop partnerships and trusts
qui drop if inlist(taxpayer_type_ind, "P", "T")
drop taxpayer_type_ind



******************************************************************************
* FIGURE 1: Number of random and targeted audits actually conducted each year*
******************************************************************************

preserve

* Identify random and targeted cases
gen byte random = (random_sig=="Y")
gen byte targeted = (random_sig=="N")

collapse (sum) randomN=random  targetedN=targeted (mean) randomPr=random targetedPr=targeted if (inlist(status_ind,"O","S") | (_merge == 2)), by(tax_year) fast

restore


********************************
*TABLE 2: Random audit outcomes*
********************************

preserve

qui keep if (yrssince == 0)
keep if (random_sig == "Y") & (status_ind == "S")

gen byte correct = (stlmnt_class_ind == "A")
gen byte yieldnonpositive = (stlmnt_class_ind == "B")
gen byte yieldpositive = inlist(stlmnt_class_ind,"C","1","2","3")
gen double directdutyif = direct_dutyenq_amt if (direct_dutyenq_amt > 0)
gen byte directduty100 = ((direct_dutyenq_amt > 0) & (direct_dutyenq_amt <= 100)) if (direct_dutyenq_amt > 0)
gen byte directduty1000 = ((direct_dutyenq_amt > 100) & (direct_dutyenq_amt <= 1000)) if (direct_dutyenq_amt > 0)
gen byte directduty10000 = ((direct_dutyenq_amt > 1000) & (direct_dutyenq_amt <= 10000)) if (direct_dutyenq_amt > 0)
gen byte directdutytop = ((direct_dutyenq_amt > 10000) & (direct_dutyenq_amt < .)) if (direct_dutyenq_amt > 0)

# delimit ;
collapse (mean) correct yieldnonpositive yieldpositive directdutyif directduty100 directduty1000 directduty10000 directdutytop
	(sd) correct_sd=correct yieldnonpositive_sd=yieldnonpositive yieldpositive_sd=yieldpositive directdutyif_sd=directdutyif
	(sd) directduty100_sd=directduty100 directduty1000_sd=directduty1000 directduty10000_sd=directduty10000 directdutytop_sd=directdutytop
	(count) utr_no
	, fast
	;
# delimit cr

restore


*************************************
* TABLE A1: Audit lags and durations*
*************************************

preserve

qui keep if (yrssince == 0)
qui keep if (random_sig == "Y") & (status_ind == "S")

gen int daystoauditstart = enqry_start_date - retrec_l
replace daystoauditstart = . if (daystoauditstart < 0)
gen int monthstoauditstart = int(daystoauditstart/30)

gen int auditduration = enqry_closure_date - enqry_start_date + 1
replace auditduration = . if (auditduration < 1)
gen int auditdurationmonths = int(auditduration/30)
replace auditdurationmonths = . if (auditduration < 0)

gen int daystoauditend = daystoauditstart + auditduration
gen int monthstoauditend = monthstoauditstart + auditdurationmonths

# delimit ;
collapse (mean) monthstoauditstart auditdurationmonths monthstoauditend
	(sd) monthstoauditstart_sd=monthstoauditstart auditdurationmonths_sd=auditdurationmonths monthstoauditend_sd=monthstoauditend
	(p50) monthstoauditstart_p50=monthstoauditstart auditdurationmonths_p50=auditdurationmonths monthstoauditend_p50=monthstoauditend
	(p75) monthstoauditstart_p75=monthstoauditstart auditdurationmonths_p75=auditdurationmonths monthstoauditend_p75=monthstoauditend
	(p90) monthstoauditstart_p90=monthstoauditstart auditdurationmonths_p90=auditdurationmonths monthstoauditend_p90=monthstoauditend
	(count) utr_no
	, fast
	;
# delimit cr

restore


exit
